package de.gameduell.framework.game.billard.view {
	import de.gameduell.framework.game.billard.controller.AnimationController;
	import de.gameduell.framework.game.billard.controller.IBillardController;
	import de.gameduell.framework.game.billard.controller.PhysicController;
	import de.gameduell.framework.game.billard.model.AnimationStep;
	import de.gameduell.framework.game.billard.model.Ball;
	import de.gameduell.framework.game.billard.model.BillardSettings;
	import de.gameduell.framework.game.billard.model.ShootData;
	import de.gameduell.framework.game.watch.SingleStopWatch;
	import de.gameduell.framework.gui.button.Button;
	import de.gameduell.framework.gui.button.ButtonEvent;
	import de.gameduell.framework.gui.button.RadioButtonGroup;
	import de.gameduell.framework.gui.button.ToggleButton;
	
	import org.cove.ape.AbstractParticle;
	import org.cove.ape.Vector;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.Timer;	

	/**
	 * @author cbra
	 */
	public class BaseAiming {
		protected var content:Sprite;
		protected var whiteBall:Ball;
		protected var ghostBallTimer:Timer;
		protected var ghostBall:Ball = null;
		protected var physicController:PhysicController;
		protected var direction:Vector;		
		protected var ghostView:GhostBallView;
		protected var ghostPath:Shape;
		protected var reflectionPath:Shape;
		protected var ghostReflectionPath:Shape;
		protected var hitBallPath:Shape;
		protected var animationController:AnimationController;
		protected var shootData:ShootData;
		protected var qhostPathContainer:Sprite;
		protected var settings:BillardSettings;
		protected var controller:IBillardController;
		protected var tableBounds:Sprite;
		protected var queueAiming:Boolean;
		protected var shootButton:Button;
		protected var eightBallHoleSelectionUser:RadioButtonGroup;
		protected var ghostPathMask:Sprite;
//		protected var powerView:PowerView;
//		protected var effetView:EffetView;
//		protected var angleView:AngleView;
		protected var singleStopWatch:SingleStopWatch;
		protected var holeHighlights:Sprite;
		
		private var queue:IQueue;
	
		public function BaseAiming(aContent:Sprite,aController:IBillardController, aQueue:IQueue) {
			controller		    = aController;
			physicController 	= controller.physicController;
			animationController = controller.animationController;
			shootData 			= controller.shootData;
			settings 			= controller.billardSettings;
			queue				= aQueue;
			singleStopWatch  	= SingleStopWatch.getInstance();			
	
			whiteBall = physicController.whiteBall;
			ghostBall = physicController.ghostBall;

			content   = aContent;
			
			shootButton = new Button(content.getChildByName("shootButton") as Sprite);
			holeHighlights = content.getChildByName("holeHighlights") as Sprite;
			holeHighlights.visible = false;

			tableBounds = content.getChildByName("tableBounds") as Sprite;
//			queue = new Queue(content,controller);

			
			ghostBallTimer = new Timer(40,0);
			
			direction = new Vector();
			ghostView = new GhostBallView(aContent,physicController);
			
			initPath();
			
			createHoleSelectionButtons();
			
//			powerView = new PowerView(content,controller);
//			effetView = new EffetView(content,controller);
//			angleView = new AngleView(content,controller);	
//	
//			
//			if(controller.isStandalone){
//				powerView.activate();
//				effetView.activate();
//			}
		}
		
		protected function animOver(event:Event = null):void{	
			eightBallHoleSelectionUser.reset();
			shootData.reset();	
		}
		
		protected function createHoleSelectionButtons():void{
			// radioButtons for the user
			
			eightBallHoleSelectionUser = new RadioButtonGroup();
			for(var i:int=0;i<6;i++){
				var clipUser:Sprite = content.getChildByName("eightBallUser_"+i) as Sprite;
				var buttonUser:ToggleButton = new ToggleButton(clipUser,String(i));
				buttonUser.addEventListener(ButtonEvent.CLICK, onHoleSelection);
				eightBallHoleSelectionUser.addButton(buttonUser);
			}
			eightBallHoleSelectionUser.active = false;
		}
		
		// ABSTRACT
		protected function onHoleSelection(event:ButtonEvent):void {

		}
		
		// creates the ghostPath and the position of the ghostball
		protected function createGhostPath(event:Event = null):void{
			if (!animationController.running) {
				ghostPathMask.visible = true;	
				queue.update();
				physicController.calcGhost();

				var collsisionIndex:int = ghostBall.animationLength-settings.getValue(BillardSettings.KEY_GHOSTSTEPS_AFTER_COLLISION)-1;

				if(collsisionIndex <= ghostBall.animationLength && collsisionIndex>=0){
					var collisionStep:AnimationStep = ghostBall.animationPath[collsisionIndex];
					if(collisionStep.collisionPosition != null){
						ghostView.x = collisionStep.collisionPosition.x;
						ghostView.y = collisionStep.collisionPosition.y;
						ghostView.visible = true;
						ghostPathMask.visible = true;	
					}else{	
						ghostView.x = collisionStep.position.x;
						ghostView.y = collisionStep.position.y;
						ghostView.visible = false;
						ghostPathMask.visible = false;	
					}
					paintGhostPath(collsisionIndex);	
					
					if(queue.visible == false){
						queue.visible = true;
					}
				}
			}
		}
		
		protected function initPath():void{		
			ghostPath = new Shape();
			ghostPath.graphics.lineStyle(1,0,0.3);
			
			hitBallPath = new Shape();
			hitBallPath.graphics.lineStyle(1,0,0.3);
			
			reflectionPath = new Shape();
			reflectionPath.graphics.lineStyle(2,0,0.3);
					
			ghostReflectionPath = new Shape();
			ghostReflectionPath.graphics.lineStyle(2,0xFFFFFF,0.3);
			
			qhostPathContainer = content.getChildByName("ghostPath") as Sprite;
			qhostPathContainer.addChild(ghostReflectionPath);
			qhostPathContainer.addChild(reflectionPath);
			qhostPathContainer.addChild(ghostPath);
			qhostPathContainer.addChild(hitBallPath);
			
			ghostPathMask =  content.getChildByName("maskTest") as Sprite;
			qhostPathContainer.mask = ghostPathMask;
			ghostPathMask.mask = ghostView.ballMask;
			ghostView.visible = false;
		}
		
				// paints the ghostPath
		protected function paintGhostPath(collisionIndex:int):void {	
			var pos:Vector;
			clearSteps();
			var collisionStep:AnimationStep = ghostBall.animationPath[collisionIndex];
			// paint from whiteball to ghostball endpos
			ghostPath.graphics.moveTo(whiteBall.step.position.x, whiteBall.step.position.y);
			if(collisionStep.collisionPosition != null){
//			if(collisionStep.collidedParticles.length > 0){
				for (var i:uint = 0;i <= collisionIndex; i++) {
					pos = AnimationStep(ghostBall.animationPath[i]).position;		
					ghostPath.graphics.lineTo(pos.x, pos.y);
				}
			}else{		
				for (var k:uint = 0;k <= collisionIndex; k++) {
					pos = AnimationStep(ghostBall.animationPath[k]).position;
					ghostPath.graphics.lineStyle(1,0,0.3-(k/collisionIndex)/3);		
					ghostPath.graphics.lineTo(pos.x, pos.y);		
				}	
			}

			if(collisionStep.collisionPosition != null){
				// reflection from the ghostBall
				paintReflection(ghostBall,collisionIndex);		
				var collidedparticle:AbstractParticle = collisionStep.collidedParticles[0];
				if(collidedparticle is Ball) {
					//reflection of the collided objectBall
					paintReflection(Ball(collidedparticle),collisionIndex);	
				}
			}
		}

		// paints the direction vectors of teh balls after collision
		protected function paintReflection(ball:Ball,collisionIndex:int):void {
			var collisionStep:AnimationStep = ball.animationPath[collisionIndex];
			var endPos:Vector   = ball.position;
			var startPos:Vector = collisionStep.collisionPosition;
			var shape:Shape		= ball == ghostBall ? ghostReflectionPath : reflectionPath;		
			shape.graphics.moveTo(startPos.x, startPos.y);
			var diffVector:Vector = endPos.minus(startPos);
			diffVector = diffVector.normalize();
			diffVector = diffVector.mult(40);
			diffVector = diffVector.plus(startPos);
			shape.graphics.lineTo(diffVector.x, diffVector.y);		
		}
				
		// removes the ghostPath
		protected function clearSteps():void{
			ghostPath.graphics.clear();	
			ghostPath.graphics.lineStyle(1,0,0.3);
			hitBallPath.graphics.clear();	
			hitBallPath.graphics.lineStyle(1,0,0.3);	
			reflectionPath.graphics.clear();	
			reflectionPath.graphics.lineStyle(2,0,0.3);
			ghostReflectionPath.graphics.clear();	
			ghostReflectionPath.graphics.lineStyle(2,0xFFFFFF,0.3);		
		}
		
	}
}
